﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class MessageInfo : System.Web.UI.Page
{
    News currentNews = new News();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] == null || Session["roleID"] == null)
        {
            Response.Redirect("Login.aspx");
            Response.End();
        }       
        if (!Page.IsPostBack)
        {
                      
            BindNews();
            BindFiles();
            BindToRPNoReadMsg();
            BindToDdlFlowYear();
            BindToDdlYCYear();
            BindToFlowSteps();
            BindCheckYC();
            currentNews.AutoDelNews();
        }

    }
    //绑定未读信息
    protected void BindToRPNoReadMsg()
    {
        string userName = Session["userName"].ToString();
        messages msg = new messages();
        DataSet ds = msg.SearchNoReadMsg();        
        rpNoReadMsg.DataSource = ds.Tables["NoReadMessages"].DefaultView;
        rpNoReadMsg.DataBind();
        int count = Convert.ToInt32(msg.CountNoReadMsg());
        lblMsgCount.Text = count.ToString();
        if (count == 0)
        {
            rpNoReadMsg.Visible = false;
        }
        
    }
    //绑定未处理异常信息
    protected void BindCheckYC()
    {
        string userName = Session["userName"].ToString();
        string roleID = Session["roleID"].ToString();
        string userCondition = "";        
        userCondition = " and CURRENTOPERATER='" + userName + "'";
       
        string dataYear = ddlYCYear.SelectedValue.ToString();
        string YCMsgTabName = "YCMS" + dataYear;        
        string sqlString = "select top 10 * from " + YCMsgTabName + " where STATUS='0'" + userCondition+"order by ADDDATE DESC,AUTOID DESC";
        DBOperation DBO = new DBOperation();
        DataSet ds = DBO.GetDataSet(sqlString, "CheckYC");
        RpYCList.DataSource = ds.Tables["CheckYC"].DefaultView;
        RpYCList.DataBind();

    }
    //绑定流程列表数据
    protected void BindToFlowSteps()
    {
        string LCtableName = "LC" + ddlFlowYear.SelectedValue.ToString();
        string LCBZtableName = "LCBZ" + ddlFlowYear.SelectedValue.ToString();
        string DataTableName = "DATA" + ddlFlowYear.SelectedValue.ToString();
        string userName = Session["userName"].ToString();
        string roleID = Session["roleID"].ToString();
        string userCondition = "";        
        userCondition = " and LCBZ.OPERATORID='" + userName + "'";

        string sqlString = "select top 10 * from " + LCtableName + " as LC inner join " + LCBZtableName + " as LCBZ on LC.TRUEFLOWID=LCBZ.TRUEFLOWID and LC.CURRENTSTEP=LCBZ.SEQ";
        sqlString += " where LC.STATUS='0'" + userCondition;
        sqlString += " order by LC.BEGINDATE DESC";
        DBOperation DBO = new DBOperation();
        DataSet ds = DBO.GetDataSet(sqlString, "temp");
        RpFlowList.DataSource = ds.Tables["temp"].DefaultView;
        RpFlowList.DataBind();
    }
    protected void BindToDdlYCYear()
    {
        BasicClass BC = new BasicClass();
        int currentYear = Convert.ToInt32(DateTime.Now.Year.ToString());
        Hashtable htYear = BC.GetDataYear();
        ddlYCYear.DataSource = htYear;
        ddlYCYear.DataTextField = "value";
        ddlYCYear.DataValueField = "key";
        ddlYCYear.DataBind();
        ddlYCYear.SelectedValue = currentYear.ToString();
    }
    protected void BindToDdlFlowYear()
    {
        BasicClass BC = new BasicClass();
        int currentYear = Convert.ToInt32(DateTime.Now.Year.ToString());
        Hashtable htYear = BC.GetDataYear();
        ddlFlowYear.DataSource = htYear;
        ddlFlowYear.DataTextField = "value";
        ddlFlowYear.DataValueField = "key";
        ddlFlowYear.DataBind();
        ddlFlowYear.SelectedValue = currentYear.ToString();
    }

    protected void ddlFlowYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindToFlowSteps();
    }
    protected void ddlYCYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCheckYC();
    }
    /// <summary>
    /// 绑定新闻
    /// </summary>
    protected void BindNews()
    {

        DataSet ds = currentNews.ReadAllNews();
        dgNews.DataSource = ds.Tables["News"];
        dgNews.DataBind();

    }
    protected void dgNews_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        dgNews.CurrentPageIndex = e.NewPageIndex;
        BindNews();
    }
    /// <summary>
    /// 绑定文件
    /// </summary>
    protected void BindFiles()
    {
        CommFiles cf = new CommFiles();
        DataView dv = cf.ReadFilesRecord().Tables["commFiles"].DefaultView;
        dv.Sort = "fileID desc";
        dgFiles.DataSource = dv;
        dgFiles.DataBind();
    }
    protected void dgFiles_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        dgFiles.CurrentPageIndex = e.NewPageIndex;
        BindFiles();
    }
    /// <summary>
    /// 生成下载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dgFiles_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        foreach (DataGridItem dg in dgFiles.Items)
        {
            Label lb = (Label)dg.FindControl("lblLoad");
            string fileName = dg.Cells[1].Text;
            string encodeFilePath = Server.UrlEncode("/common/"+fileName);
            lb.Text = "<a href='DownLoad.aspx?filepath=" + encodeFilePath + "'>点击下载</a>";
        }
    }
  
  
}
